import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/tastyIndex',
      meta: {authentication: true, index: 0}
    },
    {
      path: '*',
      redirect: '/404',
      meta: {title: '404', authentication: false, index: 0}
    },
    {
      name: "test",
      path: '/test',
      component: resolve => require(['../components/test/TestIndex'], resolve),
      meta: {authentication: false, index: 0}
    },
    {
      name: "404",
      path: '/404',
      component: resolve => require(['../views/404'], resolve),
      meta: {authentication: false, index: 0}
    },
    {
      name: "403",
      path: '/403',
      component: resolve => require(['../views/403'], resolve),
      meta: {authentication: false, index: 0}
    },
    {
      name: "500",
      path: '/500',
      component: resolve => require(['../views/500'], resolve),
      meta: {authentication: false, index: 0}
    },
    {
      name: "login",
      path: '/login',
      component: resolve => require(['../views/Login'], resolve),
      meta: {authentication: false, index: 0}
    },
    {
      path: '/',
      component: resolve => require(['../views/Home'], resolve),
      meta: {title: '自述文件'},
      children: [
        {
          name: "tastyIndex",
          path: '/tastyIndex',
          component: resolve => require(['../components/TastyIndex'], resolve),
          meta: {authentication: true, descr: '系统主页', index: 0}
        },
        {
          name: "userInfoIndex",
          path: '/userInfoIndex',
          component: resolve => require(['../components/user_info/UserInfoIndex'], resolve),
          meta: {authentication: true, descr: '个人主页', index: 1}
        },
        {
          name: "brandReportIndex",
          path: '/brandReportIndex',
          component: resolve => require(['../components/report/brand/BrandReportIndex'], resolve),
          meta: {authentication: true, descr: "品牌报表主页", index: 1}
        },
        {
          name: "storeReportIndex",
          path: '/storeReportIndex',
          redirect: '/storeReportTabIntoInventory',
          component: resolve => require(['../components/report/store/StoreReportIndex'], resolve),
          meta: {authentication: true, descr: "门店报表主页", index: 1},
          children: [{
            name: "storeReportTabIntoInventory",
            path: '/storeReportTabIntoInventory',
            component: resolve => require(['../components/report/store/StoreReportTabIntoInventory.vue'], resolve),
            meta: {authentication: true, descr: "门店报表入库", index: 2},
          }, {
            name: "storeReportTabTotalInventory",
            path: '/storeReportTabTotalInventory',
            component: resolve => require(['../components/report/store/StoreReportTabTotalInventory'], resolve),
            meta: {authentication: true, descr: "门店报表总库存", index: 2},
          }, {
            name: "storeReportTabInventoryTrace",
            path: '/storeReportTabInventoryTrace',
            component: resolve => require(['../components/report/store/StoreReportTabInventoryTrace'], resolve),
            meta: {authentication: true, descr: "门店报表出库数量", index: 2},
          }]
        },
        {
          name: "configurationIndex",
          path: '/configurationIndex',
          component: resolve => require(['../components/configuration/ConfigurationIndex'], resolve),
          meta: {authentication: true, descr: '配置主页', index: 1}
        },
        {
          name: "forewarningIndex",
          path: '/forewarningIndex',
          component: resolve => require(['../components/inventory_warning/ForewarningIndex'], resolve),
          meta: {authentication: true, descr: '预警主页', index: 1},
        },
        {
          name: "materialIndex",
          path: '/materialIndex',
          component: resolve => require(['../components/material/MaterialIndex'], resolve),
          meta: {authentication: true, descr: '物料主页', index: 1}
        },
        {
          name: "inventoryTraceIndex",
          path: '/inventoryTraceIndex',
          component: resolve => require(['../components/inventory_trace/InventoryTraceIndex'], resolve),
          meta: {authentication: true, descr: '出库主页', index: 1}
        },
        {
          name: "inventoryIndex",
          path: '/inventoryIndex',
          component: resolve => require(['../components/inventory/InventoryIndex'], resolve),
          meta: {authentication: true, descr: '入库主页', index: 1}
        },
        {
          name: "inventoryTraceList",
          path: '/inventoryTraceList',
          component: resolve => require(['../components/inventory_trace/InventoryTraceList'], resolve),
          meta: {authentication: true, descr: '出库记录', index: 2}
        },
        {
          name: "inventoryList",
          path: '/inventoryList',
          component: resolve => require(['../components/inventory/InventoryList'], resolve),
          meta: {authentication: true, descr: '入库记录', index: 2}
        },
        {
          name: "materialList",
          path: '/materialList',
          component: resolve => require(['../components/material/MaterialList'], resolve),
          meta: {authentication: true, descr: '物料记录', index: 2}
        },
        {
          name: "dailyMaterialInventoryReport",
          path: '/dailyMaterialInventoryReport',
          component: resolve => require(['../components/report/store/DailyMaterialInventoryReport'], resolve),
          meta: {authentication: true, descr: '物料出入库统计', index: 2}
        },
        {
          name: "pushInventoryForm",
          path: '/pushInventoryForm',
          component: resolve => require(['../components/inventory/PushInventoryForm'], resolve),
          meta: {authentication: true, descr: '入库表单', index: 100}
        },
        {
          name: "popupInventoryForm",
          path: '/popupInventoryForm/:batchNo',
          component: resolve => require(['../components/inventory_trace/PopupInventoryForm'], resolve),
          meta: {authentication: true, descr: '出库表单', index: 100}
        },
        {
          name: "inventoryDetail",
          path: '/inventoryDetail',
          component: resolve => require(['../components/inventory/InventoryDetail'], resolve),
          meta: {authentication: true, descr: '批次详情', index: 100}
        }

      ]
    },
  ]
});


/**
 * 使用钩子函数对路由进行权限跳转
 * @author Canaan
 * @date 2019/8/13 15:15
 */
router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('x_asia_tasty_access_token');
  const role = sessionStorage.getItem('x_asia_tasty_login_role');
  const permission = ["STORE", "BRAND"];    //允许当前访问用户的角色
  if (to.meta.authentication === undefined) {
    throw "请给(" + to.path + ")的 authentication 属性赋值";
  }

  if (!to.meta.authentication) {
    next();
  } else if (!token || !role) {
    next('/login');
  } else if (permission.indexOf(role) <= -1) {
    next('/403');
  } else if (to.path === from.path) {
    next(false);   // 当前路径 一样
  } else {
    // 简单的判断IE10及以下不进入富文本编辑器，该组件不兼容
    if (navigator.userAgent.indexOf('MSIE') > -1 && to.path === '/editor') {
      Vue.prototype.$alert('vue-quill-editor组件不兼容IE10及以下浏览器，请使用更高版本的浏览器查看', '浏览器不兼容通知', {
        confirmButtonText: '确定'
      });
    } else {
      next();
    }
  }
});


/**
 * 页面跳转之后处理
 */
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);      //使页面能返回顶部
});


export default router;


